<?php
namespace Ntxx\Controller;
use Think\Controller;
class ExportController extends Controller {
	public function _initialize(){
		if(!session("ntxx_export")){
			$this->login();
			exit;
		}
		//parent::_initialize();
	}
	
	private function login(){
		if(IS_GET){
			$this->display("login");
		}
		else{
			$pass = I("post.password");
			if($pass=="07928505892"){
				session("ntxx_export",1);
				$this->success("登录成功");
				exit;
			}
			$this->error("登录失败");
		}
	}
	
	public function index(){
		$param = $_GET;
		foreach($param as $k=>$v){
			$param[$k] = iconv("gb2312","utf-8",$v);
			$param[$k] = str_replace("'","",$param[$k]);
		}
		$this->assign('param',$param);// 赋值数据集
		
		$map = M("Company")->create($param);
		
		foreach($map as $k=>$v){
			if($k=='name'){
				$map[$k] = array('like',"%$v%");
			}
		}
		
		
		$page = M('Company'); // 实例化User对象
		$count      = $page->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Bootstrap\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $page->where($map)->field("name,fra,round(fra/count,2) as avg_fra,name,fra,pc,count,id")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//dump($list);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function export($id=0){
		if(!$id){
			$this->error("参数错误");
		}
		$company = M("company")->field("name,fra,round(fra/count,2) as avg_fra,name,fra,pc")->getById($id);
		//dump($company);
		//exit;
		if(!$company){
			$this->error("参数错误");
		}
		$data = M("submit")->field("name,fra,status")->where(["company"=>$id])->select();
		if(count($data)==0){
			$this->error("该单位没有任何人答题");
		}
        import("Org.Util.PHPExcel");

        \PHPExcel_Settings::setCacheStorageMethod(\PHPExcel_CachedObjectStorageFactory::cache_to_discISAM);
        $objPHPExcel = new \PHPExcel();
        //设置活动表
        $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet = $objPHPExcel->getActiveSheet();
		
        $c = 1;
		$objActSheet->mergeCells('A1:C1');
		$objActSheet->getStyle('A1')->getAlignment()->setWrapText(true);
		$objActSheet->getRowDimension(1)->setRowHeight(150);
		$objActSheet->setCellValueExplicitByColumnAndRow(0,$c,"单位代码：".$id."\n单位名称：".$company["name"]."\n登记人数：".$company["pc"]."\n总答题人数：".count($data)."\n总分：".$company["fra"]."\n平均分：".$company["avg_fra"]."\n注：平均分是总分除以登记人数或实际答题人数高的为准，实际答题人数以答题已完成的人数为准");
		
		
		$c+=1;
        $objActSheet->setCellValueExplicitByColumnAndRow(0,$c,"姓名");
        $objActSheet->setCellValueExplicitByColumnAndRow(1,$c,"分数");
        $objActSheet->setCellValueExplicitByColumnAndRow(2,$c,"是否完成");
		foreach($data as $v){
			$c+=1;
            $objActSheet->setCellValueExplicitByColumnAndRow(0,$c,$v["name"]);
            //$objActSheet->setCellValueExplicitByColumnAndRow(1,$c,$v["fra"]);
			$objActSheet->setCellValue('B'.$c, $v["fra"]);
            $objActSheet->setCellValueExplicitByColumnAndRow(2,$c,$v["status"]?"是":"否");
		}
		
		
		$fileName = '党史知识'.$company["name"].date('YmdHis');
        $encoded_filename = urlencode($fileName);
        $extname = ".xls";
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . $extname . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . $extname . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $fileName . $extname . '"');
        }
        header("Content-Transfer-Encoding:binary");

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
	}
}